package com.anhry.app.safety.manage.system.struts.action;

import java.util.ArrayList;
import java.util.List;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.system.bean.TbSysFun;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.struts.base.SystemBaseAction;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.OnlineUsers;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.online.ApplicationConstants;
import com.opensymphony.xwork2.ActionContext;

/**
 * @date:2012-5-15
 * @remark:
 * @author:dongbin
 */

@Controller
@Scope("prototype")
public class IndexAction extends SystemBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List mainFunList;
	private List funList;
	private TbSysFun fun;
	private String funId;
	
	/**
	 * 头部大菜单
	 * @return
	 */
	public String header(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			List funList = this.systemService.getFunByUser(user);
			this.contextPvd.setSessionAttr(Util.SESSION_FUNCTION, funList);//将用户所有菜单放入session中
			List<TbSysFun> mainFunList = new ArrayList<TbSysFun>();
			if(null!=funList && funList.size()>0){
				for(int i=0;i<funList.size();i++){
					TbSysFun fun = (TbSysFun)funList.get(i);
					if("1".equals(fun.getFunParentId().toString())){
						mainFunList.add(fun);
					}
				}
			}
			this.setMainFunList(mainFunList);
			return "header";
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("indexAction:indexHeader()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 左侧菜单
	 * @return
	 */
	@SuppressWarnings("unchecked")//不会出现黄色提示
	public String left(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			List funList = (List)this.contextPvd.getSessionAttr(Util.SESSION_FUNCTION);
			List list = new ArrayList();
			if(null!=this.funId && this.funId.length()>0){
				TbSysFun fun = (TbSysFun)this.utilService.getObjectById(TbSysFun.class, Long.parseLong(this.funId));
				this.setFun(fun);
				fun.setFunParentId(Long.parseLong("-1"));
				list = this.systemService.getChildFun(fun, funList);
				list.add(fun);
				this.setFunList(list);
			}
			return "left";
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("indexAction:indexLeft()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	public String onlineUsers() {
		ActionContext.getContext().put("onlineUsers", ApplicationConstants.ONLIE_USERS);
	
		return SUCCESS;
	}
	/**
	 * 尾部
	 * @return
	 */
	public String bottom(){
		return "bottom";
	}

	public TbSysFun getFun() {
		return fun;
	}

	public void setFun(TbSysFun fun) {
		this.fun = fun;
	}

	public String getFunId() {
		return funId;
	}

	public void setFunId(String funId) {
		this.funId = funId;
	}

	public List getFunList() {
		return funList;
	}

	public void setFunList(List funList) {
		this.funList = funList;
	}

	public List getMainFunList() {
		return mainFunList;
	}

	public void setMainFunList(List mainFunList) {
		this.mainFunList = mainFunList;
	}

}
